package com.mz.ozy.adapter;

import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mz.ozy.R;
import com.mz.ozy.bean.CartListBean;
import com.mz.ozy.bean.ShoppingCartListBean;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Created by Administrator on 2018/11/20.
 */

public class ShoppingCartAdapter extends BaseQuickAdapter<CartListBean, BaseViewHolder> {

    private boolean isEdit;

    public ShoppingCartAdapter(@Nullable List<CartListBean> data) {
        super(R.layout.view_item_car_goods, data);
//        addTestData();
        isEdit = false;
    }

    private void addTestData() {
        getData().add(new CartListBean());
        getData().add(new CartListBean());
        getData().add(new CartListBean());
    }

    @Override
    protected void convert(BaseViewHolder helper, final CartListBean item) {
        helper.addOnClickListener(R.id.iv_minus)
                .addOnClickListener(R.id.iv_add)
                .addOnClickListener(R.id.rl_select);

        TextView tvTitle = helper.getView(R.id.tv_title);
        ImageView ivIcon = helper.getView(R.id.iv_icon);
        TextView tvInfo = helper.getView(R.id.tv_info);
        TextView tvMoney = helper.getView(R.id.tv_money);
        TextView tvGoodsCount = helper.getView(R.id.tv_goods_count);
        ImageView ivSelect = helper.getView(R.id.iv_select);

        final String img_zt = item.getImg_zt();
        if (!TextUtils.isEmpty(img_zt)) {
            final String[] img_ztarr = img_zt.trim().split(",");
            Glide.with(mContext).load(img_ztarr[0])
                    .transition(DrawableTransitionOptions.withCrossFade())//适用于Drawable
                    .into(ivIcon);
        } else {
            GlideUtils.loadImg(mContext, ivIcon, R.mipmap.logo);
        }

        if (item.check()) {
            ivSelect.setImageResource(R.mipmap.cb_red_select);
        } else {
            ivSelect.setImageResource(R.mipmap.cb_red_normal);
        }

        tvMoney.setText(getPrice(item));
        tvGoodsCount.setText(String.valueOf(item.getNum()));
        tvTitle.setText(item.getTradename());
        tvInfo.setText(item.getName());

    }

    @NotNull
    private String getPrice(CartListBean item) {
        String price = "";
        if (!TextUtils.isEmpty(item.getPrice()) && Double.parseDouble(item.getPrice()) != 0.00) {
            price += "¥";
            price += item.getPrice();
        }
        if (!TextUtils.isEmpty(item.getMarket_price()) && Double.parseDouble(item.getMarket_price()) != 0.00) {
            if (!TextUtils.isEmpty(price)) {
                price += "+";
                price += item.getMarket_price();
                price += "积分";
            } else {
                price += item.getMarket_price();
                price += "积分";
            }
        }
        if (TextUtils.isEmpty(price)) {
            price = "¥0.00";
        }
        return price;
    }

    /**
     * 设置 是否为编辑模式
     *
     * @param b
     */
    public void setEdit(boolean b) {
        isEdit = b;
        notifyDataSetChanged();
    }
}
